﻿Imports System.Reflection
Imports Microsoft.Office.Interop
Imports System.Timers


Public Class Form1

    Private Sub tmrMailCheck_Tick(sender As System.Object, e As System.EventArgs)
        ' Create Outlook application.
        Dim oApp As Outlook.Application = New Outlook.Application()
        '' Start Outlook.
        '' If it is already running, you'll use the same instance...
        'Dim oApp As Outlook.Application
        'oApp = CreateObject("Outlook.Application")

        Dim SAPI
        SAPI = CreateObject("SAPI.spvoice")

        ' Get Mapi NameSpace.
        Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
        oNS.Logon("Outlook", Missing.Value, False, True) ' TODO:

        ' Get Messages collection of Inbox.
        Dim oInbox As Outlook.MAPIFolder
        oInbox = oNS.GetFolder("Facilities Helpdesk/Inbox")
        Dim oItems As Outlook.Items = oInbox.Items
        'Console.WriteLine("Total : " & oItems.Count)

        ' Get unread e-mail messages.
        oItems = oItems.Restrict("[Unread] = true")
        'MsgBox("Total Unread : " & oItems.Count)

        ' Loop each unread message.
        Dim oMsg As Outlook.MailItem
        Dim i As Integer

        For i = 1 To oItems.Count
            oMsg = oItems.Item(i)
            SAPI.Speak("New helpdesk mail from " + oMsg.SenderName)
            'Console.WriteLine(i)
            'Console.WriteLine(oMsg.SenderName)
            'Console.WriteLine(oMsg.Subject)
        Next

        ' Log off.
        oNS.Logoff()

        ' Clean up.
        oApp = Nothing
        oNS = Nothing
        oItems = Nothing
        oMsg = Nothing
        tmrCallCheck.Start()
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs)


    End Sub

    Private Sub tmrCallCheck_Tick(sender As System.Object, e As System.EventArgs)
        tmrCallCheck.Stop()
        ' Create Outlook application.
        Dim oApp2 As Outlook.Application = New Outlook.Application()
        Dim SAPI
        SAPI = CreateObject("SAPI.spvoice")

        ' Get Mapi NameSpace.
        Dim oNS2 As Outlook.NameSpace = oApp2.GetNamespace("mapi")
        oNS2.Logon("Outlook", Missing.Value, False, True) ' TODO:

        ' Get Messages collection of Inbox.
        Dim oInbox2 As Outlook.MAPIFolder = oNS2.Folders("Facilities Helpdesk").Folders("Calls")
        Dim oItems2 As Outlook.Items = oInbox2.Items
        'Console.WriteLine("Total : " & oItems.Count)

        ' Get unread e-mail messages.
        oItems2 = oItems2.Restrict("[Unread] = true")
        'MsgBox("Total Unread : " & oItems.Count)

        ' Loop each unread message.
        Dim oMsg2 As Outlook.MailItem
        Dim j As Integer

        For j = 1 To oItems2.Count
            oMsg2 = oItems2.Item(j)
            SAPI.Speak("Missed call notification from " + oMsg2.SenderName)
            'Console.WriteLine(i)
            'Console.WriteLine(oMsg.SenderName)
            'Console.WriteLine(oMsg.Subject)
        Next

        ' Log off.
        oNS2.Logoff()

        ' Clean up.
        oApp2 = Nothing
        oNS2 = Nothing
        oItems2 = Nothing
        oMsg2 = Nothing
        tmrUserCheck.Start()

    End Sub


    'User Inbox Check
    Private Sub tmrUserCheck_Tick(sender As System.Object, e As System.EventArgs) Handles tmrUserCheck.Tick
        tmrUserCheck.Stop()
        ' Create Outlook application.
        Dim oApp3 As Outlook.Application = New Outlook.Application()
        Dim SAPI
        SAPI = CreateObject("SAPI.spvoice")

        ' Get Mapi NameSpace.
        Dim oNS3 As Outlook.NameSpace = oApp3.GetNamespace("mapi")
        oNS3.Logon("Outlook", Missing.Value, False, True) ' TODO:

        ' Get Messages collection of Inbox.
        Dim oInbox3 As Outlook.MAPIFolder
        oInbox3 = oNS3.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        Dim oItems3 As Outlook.Items = oInbox3.Items
        MsgBox("Total : " & oItems3.Count)

        ' Get unread e-mail messages.
        oItems3 = oItems3.Restrict("[Unread] = true")
        MsgBox("Total Unread : " & oItems3.Count)

        ' Loop each unread message.
        Dim oMsg3 As Outlook.MailItem
        'Dim j As Integer

        For Each oMsg3 In oItems3
            SAPI.Speak("Personal inbox message from " + oMsg3.SenderName)
            'For j = 1 To oItems3.Count
            '    oMsg3 = oItems3.Item(j)
            '    
            '    'Console.WriteLine(i)
            '    'Console.WriteLine(oMsg.SenderName)
            '    'Console.WriteLine(oMsg.Subject)
        Next

        ' Log off.
        oNS3.Logoff()

        ' Clean up.
        oApp3 = Nothing
        oNS3 = Nothing
        oItems3 = Nothing
        oMsg3 = Nothing
    End Sub
End Class
